package org.mspring.mlog.web.module.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.News;
import org.mspring.mlog.service.NewsService;
import org.mspring.mlog.web.query.NewsQueryCriterion;
import org.mspring.platform.dao.hibernate.common.CacheRule;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.dao.support.query.QueryCriterion;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月8日
 */
@Widget
@RequestMapping("/news")
public class Web_NewsWidget extends Web_AbstractWidget {
    @Autowired
    private NewsService newsService;

    @RequestMapping({ "/list", "/", "" })
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {
        Integer page = RequestUtils.getRequestParameterAsInteger(request, "page");
        Long cat = RequestUtils.getRequestParameterAsLong(request, "cat");
        String filter = RequestUtils.getRequestParameterAsString(request, "filter");

        if (page == null) {
            page = 1;
        }
        if (StringUtils.isBlank(filter)) {
            filter = "recent";
        }

        Page<News> newsPage = new Page<News>();
        newsPage.setPageNo(page);
        if ("hot".equals(filter)) {
            newsPage.setSort(new Sort("views", Sort.DESC));
        } else {
            newsPage.setSort(new Sort("createTime", Sort.DESC));
        }

        Map<String, Object> queryParams = new HashMap<String, Object>();
        if (cat != null) {
            queryParams.put("catalog.id", cat);
        }
        queryParams.put("status", News.Status.PUBLISH);
        QueryCriterion queryCriterion = new NewsQueryCriterion(queryParams);

        CacheRule cacheRule = new CacheRule("Web_NewsWidget_list");

        newsPage = newsService.listPage(queryCriterion, newsPage, cacheRule);

        model.addAttribute("page", page);
        model.addAttribute("cat", cat);
        model.addAttribute("filter", filter);
        model.addAttribute("newsPage", newsPage);
        return "skin:/news/index";
    }

    @RequestMapping("/{id}")
    public String show(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return "skin:/404";
        }
        News news = newsService.get(id);

        if (news != null) {
            String news_keyword = news.getTitle();
            String news_description = "";
            if (StringUtils.isNotBlank(news.getSummary())) {
                news_description = StringUtils.contentTransform(news.getSummary(), true, false, null, null);
            } else {
                news_description = StringUtils.contentTransform(news.getContent(), true, true, 0, 200);
                if (news_description != null) {
                    news_description = news_description.trim().replaceAll("\n", "");
                }
            }
            model.addAttribute("news_keyword", news_keyword);
            model.addAttribute("news_description", news_description);
        }
        model.addAttribute("news", news);
        return "skin:/news/show";
    }
}
